package com.itheima.cofig;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;

/**
 * @author 张义
 * @describe: 数据源的 Configuration 配置类
 * @date 2021/06/19
 */

//<context:property-placeholder location="classpath:jdbc.properties"/>  替换 xml 中的 properties 配置文件加载
@PropertySource("classpath:jdbc.properties")

public class DataSourceConfiguration {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource1") // Spring 会将当前方法的返回值以指定名称加载到 Spring 容器中
    public DataSource getC3p0Source() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean("dataSource2") // Spring 会将当前方法的返回值以指定名称加载到 Spring 容器中
    public DataSource getDruidSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

}
